package com.zyz.sunliky.controller;

import com.alibaba.fastjson.JSON;
import com.zyz.sunliky.common.ResultCommon;
import com.zyz.sunliky.service.ChangeProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


@RestController
//@CrossOrigin()
public class ChangeProjectController {

    public static final Logger logger = LoggerFactory.getLogger(ChangeProjectController.class);
    @Autowired
    ChangeProjectService projectService;

    /**
     * 自动生成项目号
     * @param projectType
     * @return
     */
    @GetMapping ("/getNum")
    //@CrossOrigin
    public String projectNum(String projectType){
        //JSONObject result = new JSONObject();
        String projectNum = projectService.projectNum(projectType);
        Map<String, String> map = new HashMap<String, String>();
        map.put("projectNum", projectNum);
        //result.put("projectNum",result);
        return JSON.toJSONString(map);
    }

    /**
     * 生成子项目号
     * @param projectId
     * @return
     */
    @GetMapping ("/subProjectNum")
    public String subProjectNum(String projectId,String projectType){

        String projectNum = projectService.subProjectNum(projectId,projectType);
        Map<String, String> map = new HashMap<String, String>();
        map.put("subProjectNum", projectNum);
        return JSON.toJSONString(map);
    }

    /**
     * 更新汇报后的项目状态
     * @param id
     * @return
     */
    @GetMapping ("/updateStatus")
    public void updateStatus(String id){

        projectService.updateStatus(id);



    }

    /**
     * 营销项目更新汇报后的项目状态
     * @param id
     * @return
     */
    @GetMapping ("/updateMarketStatus")
    public void updateMarketStatus(String id,String parentId){

        projectService.updateMarketStatus(id,parentId);

    }

    /**
     * 资本支出项目更新汇报后的项目状态
     * @param id
     * @return
     */
    @GetMapping ("/updateCapitalStatus")
    public void updateCapitalStatus(String id,String parentId){

        projectService.updateCapitalStatus(id,parentId);

    }

    @GetMapping("/getAmount")
    public ResultCommon getAmount(String code){
        try {
            BigDecimal amount=projectService.getAmount(code);
            return new ResultCommon(amount,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }


    /**
     * 项目汇报
     * @param tokenid
     * @return
     */
    @GetMapping("/report")
    public ResultCommon getProjectReport(String id,String tokenid){
        try {
            String count = projectService.getProjectReport(id,tokenid);
            return new ResultCommon(count,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }

}
